package com.android.killtencent.task;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;

import com.android.killtencent.BaseApp;
import com.android.killtencent.activity.LoginActivity;
import com.android.killtencent.activity.MainActivity;
import com.android.killtencent.connection.XmppConnectionHelper;

/**
 *
 * @作 用:登陆异步任务                            ``
 * @创 建 人: zhangwei
 * @日 期: 15/2/14
 * @修 改 人:
 */
public class LoginTask extends AsyncTask<Void, Void, Integer> {
    private String userName;
    private String passWord;
    private Activity activity;
    public LoginTask(String userName, String passWord, Activity activity) {
        this.userName = userName;
        this.passWord = passWord;
        this.activity = activity;
    }
    @Override
    protected Integer doInBackground(Void... params) {
        int state = XmppConnectionHelper.getInstance().login(userName, passWord);
        return state;
    }
    @Override
    protected void onPostExecute(Integer integer) {
        super.onPostExecute(integer);
        switch (integer) {
            case 0:
                BaseApp.getInstance().showToast("连接服务器失败,请检查网络");
                activity.startActivity(new Intent(activity, LoginActivity.class));
                break;
            case 1:
                activity.startActivity(new Intent(activity, MainActivity.class));
                //登陆成功后进行自动存储用户名和密码
                BaseApp.getInstance().setData("userName" , userName);
                /** 常规应该进行加密本地化存储  */
                BaseApp.getInstance().setData("passWord" , passWord);
                break;
            case 2:
                BaseApp.getInstance().showToast("网络不给力啊,该换4G了");
                activity.startActivity(new Intent(activity, LoginActivity.class));
                break;
        }
        activity.onBackPressed();
    }
}
